
$(document).ready(function () {
    var sjjdm = Common.GetRequest().sjjdm;
    getSjjfl();
    initAddData();
    if(!sjjdm){
        $('#gxsj').val(getNowFormatDate());
        getBBHAndFBRQ();
    }else{
        getSjjxx();
    }
});

//查询所有的2级数据集分类提供选择新增和编辑时 选择数据集分类
function getSjjfl(){
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        url: 'dataSet/queryAllSjjfl',
        dataType: "json",
        async: false,
        success: function (jsonData) {
            if (jsonData.code == 200) {
                $.each(jsonData.sjjxx,function(index,obj) {
                    $("#sjjfldm").append("<option value='" + obj.sjjfldm + "'>" + obj.sjjflmc + "</option>");
                })
            } else {
                LayerWn.showFailAlert('服务器异常,请联系管理员');
            }
        }
    });
}

//初始化标准来源
function initAddData(){
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        data: {zblbdm: '1'},
        async: false,
        url: "element/initAddData",
        success: function (jsonData) {
            if (jsonData.code == 200) {   //查询成功
                //表单自动赋值
                console.log(jsonData);
                if(jsonData.zdbList!=null){
                    $.each(jsonData.zdbList,function(index,obj) {
                        $("#bzly").append("<option value='" + obj.zddm + "'>" + obj.zdmc + "</option>");
                    })
                }
            }
        }
    });
}

//查询最新的版本号和更新时间  提供选择新增和编辑时
function getBBHAndFBRQ(){
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        url: 'dataSet/queryByBbhAndFbrq',
        dataType: "json",
        success: function (jsonData) {
            if (jsonData.code == 200) {
                $('#bbh').val(jsonData.sjjxx.bbh);
                $('#fbrq').val(jsonData.sjjxx.fbrq);
            } else {
                LayerWn.showFailAlert('服务器异常,请联系管理员');
            }
        }
    });
}
function getNowFormatDate() {//获取当前时间
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1<10? "0"+(date.getMonth() + 1):date.getMonth() + 1;
    var strDate = date.getDate()<10? "0" + date.getDate():date.getDate();
    var currentdate = date.getFullYear() + seperator1  + month  + seperator1  + strDate
        + " "  + date.getHours()  + seperator2  + date.getMinutes()
        + seperator2 + date.getSeconds();
    return currentdate;
}

function cancel() {
    parent.layer.closeAll();
}
//把空的节点默认为null
function trim_nulls(data) {
    var y;
    for (var x in data) {
        y = data[x];
        y   =   y.replace(/^\s+|\s+$/g,"");
        if (y ==="") {
            data[x]=null;
        }
    }
    return data;
}

function  saveSjjxx() {
    var sjjdm = Common.GetRequest().sjjdm;

    var data = $('form').serializeJson();
    //把""改成null，让后台非空字段报错
    data=trim_nulls(data);
    var url = 'dataSet/saveDataSet';
    if (sjjdm) {
        url = 'dataSet/updateDataSet';
        data.gxsj=getNowFormatDate();
    }
    var formJSON = JSON.stringify(data);

    $.ajax({
        type: "POST",
        contentType: 'application/json',
        dataType: "json",
        url: url,
        data: formJSON,
        success: function (jsonData) {
            if (jsonData.code == 200) {
                if (jsonData.res == "1") {
                    LayerWn.showSucAlert('保存成功！');

                    for (var k = 0; k < parent.frames.length; k++) {
                        if (parent.frames[k].frameElement.src.indexOf("psms-sjjgl.html") != -1)
                            parent.frames[k].searchTree();
                    }

                    setTimeout(function () {
                        parent.layer.closeAll()
                    }, 1000);
                } else if (jsonData.res == "-1") {
                    LayerWn.showSucAlert('已存在！');
                }
            } else {
                LayerWn.showFailAlert('服务器异常,请联系管理员');
            }
        }
    });
}


function getSjjxx() {
    var sjjdm = Common.GetRequest().sjjdm; //获取传入的参数
    var bbh = Common.GetRequest().bbh; //获取传入的参数
    $("#sjjdm").attr("readOnly", "true");
    $.ajax({
        type: "GET",
        contentType: 'application/json',
        dataType: "json",
        data: {sjjdm: sjjdm,bbh:bbh},
        cache: false,
        async: false,
        url: "dataSet/querySjjxxBySjjdmAndBbh",
        success: function (jsonData) {
            if (jsonData.code == 200) {   //查询成功
                //表单自动赋值
                $('#sjjxx').populateForm(jsonData.sjjxx)
                //选中
                if(sjjdm && jsonData.sjjxx.sjjfldm!=null){
                    var sjjfldm=jsonData.sjjxx.sjjfldm;
                    var numbers = $("#sjjfldm").find("option"); //获取select下拉框的所有值
                    for (var j = 0; j < numbers.length; j++) {
                        if ($(numbers[j]).val() == sjjfldm) {
                            $(numbers[j]).prop("selected", "selected");
                        };
                    }
                }
                if(sjjdm && jsonData.sjjxx.bzly!=null){
                    var bzly=jsonData.sjjxx.bzly;
                    var numbers = $("#bzly").find("option"); //获取select下拉框的所有值
                    for (var j = 0; j < numbers.length; j++) {
                        console.log($(numbers[j]).val())
                        if ($(numbers[j]).val() == bzly) {
                            $(numbers[j]).prop("selected", "selected");
                        };
                    }
                }
            }
        }
    })
}